<template>
  <van-nav-bar title="用户登录" />
  <div class="form">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
        v-model="formData.userName"
        name="用户名"
        label="用户名"
        placeholder="请输入用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
        v-model="formData.passWord"
        type="password"
        name="密码"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button @click="onSubmit" round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
    </div>
</template>

<script setup>
import { reactive } from 'vue';
import { postLogin } from '../../api';
import { useRouter } from 'vue-router';
const router = useRouter()
//初始化form表单
const formData = reactive({
  userName: '',
  passWord:''
})
//提交表单
const onSubmit = async () => {
  const { data } = await postLogin(formData)
  if (data.code === 10000) {
    localStorage.setItem('h5_token', data.data.token)
    localStorage.setItem('h5_userInfo', JSON.stringify(data.data.userInfo))
    setTimeout(() => {
      router.push('/')
    }, 1000);
  }
}
</script>

<style lang="less" scoped>
.form {
  margin-top: 150px;
}
</style>
